package userMap

import (
	"appService/src/application/app/appModels"
	"github.com/gogf/gf/container/gmap"
)

//登陆的用户缓存到内存中,并且启用线程安全，开启读写锁(默认线程不安全)
var userMap *gmap.StrAnyMap = gmap.NewStrAnyMap(true)

// 设置登陆用户缓存
func Set(loginUser *appModels.UserInfo) {
	userMap.Set(loginUser.User.Uid, *loginUser)
}

//获取登陆用户缓存
func Get(userId string) *appModels.UserInfo {
	user := appModels.UserInfo{}
	val := userMap.Get(userId)
	if val != nil {
		user = val.(appModels.UserInfo)
	}
	return &user
}

// 判断用户是否在缓存中
func Exist(userId string) bool {
	return userMap.Contains(userId)
}

//删除登陆用户缓存
func Remove(userId string) {
	userMap.Remove(userId)
}
